package org.regotto.basics.prefixSumAndFiniteDiff;

import org.regotto.common.Data;

/**
 * 前缀和求某区间段和
 */
public class PrefixSumAndFiniteDiff {

    public static void main(String[] args) {
        finiteDiff();
    }

    /**
     * 让某一个区间内同时加一个数
     */
    public static void finiteDiff() {
        int[] q = Data.getInts(true);
        int[] b = new int[q.length];
        for (int i = 0; i < q.length - 1; i++) {
            I(b, i, i, q[i]);
        }
        // 2~4 区间同时加 1
        I(b, 2, 3, 1);
        for (int i = 1; i < q.length; i++) {
            q[i] = q[i - 1] + b[i];
            System.out.print(q[i - 1] + " ");
        }
    }
    public static void I(int[] q, int l, int r, int c) {
        q[l] += c;
        q[r + 1] -= c;
    }

    public static void prefixSum() {
        int[] q = Data.getInts(true);
        int[] s = new int[q.length];
        for (int i = 1; i < q.length; i++) s[i] = s[i - 1] + q[i];
        // 求 2~4 之前的和
        System.out.print(s[4] - s[2 - 1]);
    }

}
